package com.zdgk.core.control.config;

import cn.hutool.core.util.StrUtil;
import com.zdgk.core.web.entity.ConfigInfo;
import com.zdgk.core.web.mapper.ConfigInfoMapper;
import lombok.Data;
import java.util.List;

@Data
public class ControlConfigInfo {

    private String mode;


    private String host;
    private Integer port;

    /**
     * 串口名称
     */
    private String name;

    /**
     * 波特率
     */
    private Integer baudRate;

    /**
     * 发送间隔
     */
    private Integer sendInterval;

    /**
     * 从站地址
     */
    private String serverAddress;

    /**
     * 遥测测点数量
     */
    private String serverPointCount;
    /**
     * 遥信测点数量
     */
    private String serverSwitchPointCount;

    public Integer[] getServerAddressArr() {
        if (StrUtil.isBlank(this.serverAddress)) {
            return new Integer[0];
        }
        String[] stringArray = this.serverAddress.split(",");
        Integer[] integerArray = new Integer[stringArray.length];
        for (int i = 0; i < stringArray.length; i++) {
            integerArray[i] = Integer.parseInt(stringArray[i]);
        }
        return integerArray;
    }
    public Integer[] getServerPointCountArr() {
        if (StrUtil.isBlank(this.serverPointCount)) {
            return new Integer[0];
        }
        String[] stringArray = this.serverPointCount.split(",");
        Integer[] integerArray = new Integer[stringArray.length];
        for (int i = 0; i < stringArray.length; i++) {
            integerArray[i] = Integer.parseInt(stringArray[i]);
        }
        return integerArray;
    }
    public Integer[] getServerSwitchPointCountArr() {
        if (StrUtil.isBlank(this.serverSwitchPointCount)) {
            return new Integer[0];
        }
        String[] stringArray = this.serverSwitchPointCount.split(",");
        Integer[] integerArray = new Integer[stringArray.length];
        for (int i = 0; i < stringArray.length; i++) {
            integerArray[i] = Integer.parseInt(stringArray[i]);
        }
        return integerArray;
    }

    /**
     * 读取起始地址
     */
    private Integer readStartAddress;

    /**
     * 读取数量
     */
    private Integer quantity;


    /**
     * 保持寄存器读取起始地址
     */
    private Integer keepReadStartAddress;

    /**
     * 保持寄存器读取数量
     */
    private Integer keepQuantity;

    private Integer port104;
    private String forwardMode104;
    private Integer threeAdd104;
    private Integer threeSwitchAdd104;

    public void initConfigInfo(ConfigInfoMapper mapper) {
        List<ConfigInfo> configInfoList = mapper.selectList(null);
        configInfoList.forEach(configInfo -> {
            String configValue = configInfo.getConfigValue();
            switch (configInfo.getConfigKey()) {
                case "read-control-mode":
                    mode = configValue;
                    break;
                case "read-serial-name":
                    name = configValue;
                    break;
                case "read-serial-baud-rate":
                    baudRate = Integer.valueOf(configValue);
                    break;
                case "read-network-host":
                    host = configValue;
                    break;
                case "read-network-port":
                    port = Integer.valueOf(configValue);
                    break;
                case "read-serial-send-interval":
                    sendInterval = Integer.valueOf(configValue);
                    break;
                case "read-serial-server-address":
                    serverAddress = configValue;
                    break;
                case "read-serial-server-point-count":
                    serverPointCount = configValue;
                    break;
                case "read-serial-server-switch-point-count":
                    serverSwitchPointCount = configValue;
                    break;
                case "read-serial-read-start-address":
                    readStartAddress = Integer.valueOf(configValue);
                    break;
                case "read-serial-quantity":
                    quantity = Integer.valueOf(configValue);
                    break;
                case "keep-read-serial-read-start-address":
                    keepReadStartAddress = Integer.valueOf(configValue);
                    break;
                case "keep-read-serial-quantity":
                    keepQuantity = Integer.valueOf(configValue);
                    break;
                case "forward-network-port":
                    port104 = Integer.valueOf(configValue);
                    break;
                case "forward-mode":
                    forwardMode104 = configValue;
                    break;
                case "forward-three-add":
                    threeAdd104 = Integer.valueOf(configValue);
                    break;
                case "forward-three-switch-add":
                    threeSwitchAdd104 = Integer.valueOf(configValue);
                    break;
                default:
                    break;
            }

        });
    }

}
